package com.xzz.proxy;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

/**
 * @author: hhz
 * @create: 2021-12-10 14:03
 **/
public class Main {
    public static void main(String[] args) {
        Man man = new My();
        ManHandler manHandler = new ManHandler(man);
        Man proxyMan = (Man) Proxy.newProxyInstance(My.class.getClassLoader(), My.class.getInterfaces(), manHandler);
        System.out.println(proxyMan.getClass().getName());
        proxyMan.findGirl();
        createProxyClassFile(Man.class);
    }
    private static void createProxyClassFile(Class c){
        byte[] data = ProxyGenerator.generateProxyClass("$Proxy0",new Class[]{c});
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("$Proxy0.class");
            fileOutputStream.write(data);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
